﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models.BLL;
using tracker.Models;
using System.Data;
using tracker.Models.DTO;

namespace tracker.Controllers
{
    public class HandlerSearchDriverController : Controller
    {
       /// <summary>
       /// Get All Driver Info
       /// </summary>
       /// <returns></returns>
        [CallAjaxFilter]
        public JsonStringResult Index(string namekey)
        {
            string result = "{[]}";
            string _username = HttpContext.Session[Common.USER].ToString();
            string _language = HttpContext.Session[Common.LANGUAGE].ToString();
            _language = _language == string.Empty ? "vi" : _language;

            // Get information of current user
            DataTable dtGetUser = UserLoginBLL.SelectByUserName(new string[] { EUserLogin.FIELD_USERNAME },
                                                                new object[] { _username }).Tables[0];

            if (dtGetUser.Rows.Count > 0)
            {
                List<ENTDriverInfo> list = new List<ENTDriverInfo>();

                // Load data when open form
                if (namekey == null || namekey.Trim() == string.Empty)
                {
                    DataTable dt = DriverInfoBLL.SelectAll(new string[] { ENTDriverInfo.FIELD_CREATEBY }, new object[] { _username }).Tables[0];
                    foreach (DataRow item in dt.Rows)
                    {
                        ENTDriverInfo ent = new ENTDriverInfo();

                        ent.driver_age = DateTime.Parse(item["Birthday"].ToString()).ToString("yyyy-MM-dd");
                        ent.driver_cellphone = item["Cellphone"].ToString();
                        ent.driver_email = item["Email"].ToString();
                        ent.driver_id = int.Parse(item["DriverID"].ToString());
                        ent.driver_name = item["DriverName"].ToString();
                        ent.driver_phone = item["Phone"].ToString();
                        ent.driver_remarks = item["Remark"].ToString();
                        list.Add(ent);
                    }
                    if (list.Count > 0)
                    {
                        result = Utilities.getJsonString(list, "data:", 1);
                    }
                }
                // Load data when search
                else
                {
                    DataTable dt = DriverInfoBLL.SelectAllLike(new string[] { ENTDriverInfo.FIELD_CREATEBY, "keysearch" },
                                                               new object[] { _username, namekey }).Tables[0];
                    foreach (DataRow item in dt.Rows)
                    {
                        ENTDriverInfo ent = new ENTDriverInfo();

                        ent.driver_age = DateTime.Parse(item["Birthday"].ToString()).ToString("yyyy-MM-dd");
                        ent.driver_cellphone = item["Cellphone"].ToString();
                        ent.driver_email = item["Email"].ToString();
                        ent.driver_id = int.Parse(item["DriverID"].ToString());
                        ent.driver_name = item["DriverName"].ToString();
                        ent.driver_phone = item["Phone"].ToString();
                        ent.driver_remarks = item["Remark"].ToString();
                        list.Add(ent);
                    }
                    if (list.Count > 0)
                    {
                        result = Utilities.getJsonString(list, "data:", 1);
                    }
                }
            }
            dtGetUser.Dispose();
            return new JsonStringResult(result);
        }
    }
}
